/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * +----------------------------------------------------------------------
 */

layui.define(['jquery', 'bluebird', 'element', 'form', 'configure', 'configureCom', 'selectFun', 'apply'], function (exports) {
    var element = layui.element;
    var $ = layui.$;
    var form = layui.form;
    var table = layui.table;
    var configure = layui.configure;
    var configureCom = layui.configureCom;
    var selectFun = layui.selectFun;
    var url = configure.APPLIED_LIST_HTTP;//查看已确认已报审单位工程列表

    //根据受理状态码获取受理状态
    var getSlName = function (code) {
        if (code) {
            if (code == '1010') {
                return '待受理';
            } else if (code == '1020') {
                return '同意受理';
            } else {
                return '拒绝受理';
            }
        } else {
            return '';
        }
    };

    var cols = [[ //表头
        {
            title: '单位工程名称', field: 'singleProjName', templet: function (d) {
                return configureCom.getMSName(d) + '</br>' + '<span>编号：' + d.singleProjCode + '</span>';
            }
        },
        {
            title: '工程专业类别', templet: function (d) {
                return configureCom.getSpecialTypeDesc(d.specialType) + '</br>' + '<span>区划：' + selectFun.getAreaByCode(d.country) + '</span>';
            }
        },
        {
            title: '勘察/设计单位', templet: function (d) {
                var html = '';
                $.each(d.kcSjdwMsgDtos, function (index, item) {
                    var type = item.projBdstType == '1010' ? '勘' : '设';
                    html += type + ' ' + item.orgName + '</br>' + '<span>成果确认时间：' + configureCom.dateToStrAll(item.confirmDate) + '</span></br>';
                });
                return html;
            }
        },
        {
            title: '审查状态', templet: function (d) {
                var html = '';
                $(d.stjgMsgDtos).each(function (index, item) {
                    var housingConstruction = '';
                    var fireDefence = '';
                    var peopleDefence = '';
                    if (item.checkType === configure.AUDIT_ORGAN_TYPE.HOUSING_CONSTRUCTION.value) {
                        housingConstruction = item.orgName || '';
                        html += '<div>住 ' + housingConstruction + '</br>' + '<span>' + item.sgProjResult + '</span></div>';
                    }
                    if (item.checkType === configure.AUDIT_ORGAN_TYPE.FIRE_DEFENCE.value) {
                        fireDefence = item.orgName || '';
                        html += '<div>消 ' + fireDefence + '</br>' + '<span>' + item.sgProjResult + '</span></div>';
                    }
                    if (item.checkType === configure.AUDIT_ORGAN_TYPE.PEOPLE_DEFENCE.value) {
                        peopleDefence = item.orgName || '';
                        html += '<div>人 ' + peopleDefence + '</br>' + '<span>' + item.sgProjResult + '</span></div>';
                    }
                });
                return html;
            }
        },
        {
            title: '审批状态', width: 120, templet: function (d) {
                var html='';
                $(d.applyOrgStatusDTOs).each(function(index,item){
                   var type = '';
                   if(index==0){
                       type = '监';
                   }else if(index==1){
                       type='住';
                   }else if(index==2){
                       type='人';
                   }
                   html +='<p>'+type+' '+item.applyStatusName||'-'+'</p>';
                });
                return html;
            }
        },
        {title: '操作', width: 200, align: 'center', toolbar: '#barDemo'}
    ]];
    var apply = layui.apply;
    var reportedApply = {
        data: {
            searchData: {
                singleProjName: ''//单位工程工程名称
            }
        },
        render: function () {
            reportedApply.data.searchData.singleProjName = $('#singleProjName').val();
            //渲染表格
            window.tableRender = function () {
                configureCom.tableRender(url, {}, cols, false, 5).then(function (res) {
                    if (!configureCom.GetQueryString('addPadding')) {
                        parent.resetTab('reportedApply', res.count);
                    }
                });
            };
            tableRender();
        },
        eventBind: function () {
            //项目 操作
            table.on('tool(klmyTable)', function (obj) {
                var singleProjectId = obj.data.id;
                if (obj.event === 'detail') {//查看详情
                    var kcBdId = '';
                    var sjBdId = '';
                    $.each(obj.data.kcSjdwMsgDtos, function (index, item) {
                        if (item.projBdstType == '1010') {
                            kcBdId = item.projBdstId || '';
                        }
                        if (item.projBdstType == '1020') {
                            sjBdId = item.projBdstId || '';
                        }
                    });

                    //获取报审详情
                    configureCom.getAjax(configure.REPORT_MASTERIA_DETAIL_HTTP.replace('{singleProjectId}', singleProjectId), {singleProjectId: singleProjectId}).then(function (res) {
                        var jointTrial = null;//联审对象
                        var housingConstruction = null;//住建对象
                        $(res.result).each(function (index, item) {
                            if (item.acceptType === configure.AUDIT_ORGAN_TYPE.JOINT_TRIAL.value) {
                                jointTrial = item;
                            }
                            if (item.acceptType === configure.AUDIT_ORGAN_TYPE.HOUSING_CONSTRUCTION.value) {
                                housingConstruction = item;
                            }
                            //审查时间
                            if (item.adviseTime) {
                                item.adviseTime = configureCom.dateToStrAll(item.adviseTime);
                            } else {
                                item.adviseTime = '-';
                            }
                        });
                        // jointTrial.orgName = housingConstruction.orgName;
                        localStorage.bsData = JSON.stringify(res.result);

                        var _url = configure.ALL_COMMON_DETAIL + '?bsShow=1&singleProjectId=' + singleProjectId + '&kcBdId=' + kcBdId + '&sjBdId=' + sjBdId;
                        var topIndex = top.layer.open({
                            title: '查看成果',
                            type: 2,
                            btnAlign: 'c',
                            content: _url,
                            area: configureCom.Psize1,
                            end: function () {
                                top.layer.close(topIndex);
                            }
                        });
                    }, function (err) {
                        return false;
                    });
                }
                else if (obj.event === 'cancelReport') {//取消报审
                    apply.param.data.singleProjectId = singleProjectId;
                    top.layer.confirm('取消报审？', {title: '提示'}, function (index) {
                        top.layer.close(index);
                        apply.applyCancle().then(function (res) {//先取消报审
                            configureCom.loadOff();
                            if (res.code === 1) {
                                top.layer.msg('取消报审成功');
                                //取消成功后重新报审
                                if (configureCom.GetQueryString('addPadding')) {
                                    configureCom.tableReload();
                                } else {
                                    parent.refreshIframe();
                                }
                            } else {
                                top.layer.msg(res.msg);
                            }
                        });
                    });
                }
                else if (obj.event === 'repeatReport') {//重新报审
                    apply.param.data.singleProjectId = singleProjectId;
                    top.layer.confirm('重新报审？', {title: '提示'}, function (index) {
                        top.layer.close(index);

                        var title = '重新报审';
                        var _url = configure.REPORT_MASTERIA_EDIT_HTML.replace('{singleProjectId}', singleProjectId).replace('{type}', 1055);
                        configureCom.klmyPop([_url, title, '重新报审'], false, false, function (layero, index) {
                            window.saveForm = function (_data) {
                                apply.applyCancle().then(function (res) {//先取消报审
                                        if (res.code === 1) {
                                            //取消成功后重新报审
                                            configureCom.putDataAjax(configure.APPLY_CONFIRM_HTTP, _data).then(function (res) {
                                                configureCom.loadOff();
                                                if (res.code === 1) {
                                                    top.layer.msg('重新报审成功');
                                                    tableRender();
                                                } else {
                                                    top.layer.msg(res.msg);
                                                }
                                                top.layer.close(index);
                                            });
                                        }
                                        else {
                                            top.layer.msg(res.msg);
                                        }
                                    },
                                    function (err) {
                                        layer.msg('重新报审失败');
                                    });
                            };
                        });
                    });
                }
                else if (obj.event === 'auditDetail') {
                    var title = '报审详情';
                    var url = configure.REPORT_MASTERIA_DETAIL_HTML_HTTP.replace('{singleProjectId}', singleProjectId);
                    configureCom.klmyPop([url, title, 0, 4]);
                }
            });
            //监听搜索
            form.on('submit(formDemo)', function (data) {
                var fields = data.field;
                configureCom.tableRender(url, fields, cols);
                return false;
            });
        }
    };
    exports('reportedApply', reportedApply);

});